package com.aurora.blog.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.aurora.blog.domain.BlogCategories;
import com.aurora.blog.domain.BlogPostCategories;
import com.aurora.blog.domain.BlogPostTags;
import com.aurora.blog.domain.vo.BlogPostsVo;
import com.aurora.blog.mapper.BlogCategoriesMapper;
import com.aurora.blog.mapper.BlogPostCategoriesMapper;
import com.aurora.blog.mapper.BlogPostTagsMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.aurora.blog.mapper.BlogPostsMapper;
import com.aurora.blog.domain.BlogPosts;
import com.aurora.blog.service.IBlogPostsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 文章Service业务层处理
 * 
 * @author aurora
 * @date 2024-06-04
 */
@Service
public class BlogPostsServiceImpl implements IBlogPostsService 
{
    @Autowired
    private BlogPostsMapper blogPostsMapper;
    @Autowired
    private BlogPostCategoriesMapper blogPostCategoriesMapper;
    @Autowired
    private BlogPostTagsMapper blogPostTagsMapper;

    /**
     * 查询文章
     * 
     * @param postId 文章主键
     * @return 文章
     */
    @Override
    public BlogPostsVo selectBlogPostsByPostId(Long postId)
    {
        BlogPostsVo blogPostsVo = new BlogPostsVo();
        BlogPosts blogPosts = blogPostsMapper.selectBlogPostsByPostId(postId);
        BeanUtils.copyProperties(blogPosts,blogPostsVo);
        // 根据帖子ID查询对应的分类信息
        BlogPostCategories blogPostCategories = blogPostCategoriesMapper.selectBlogPostCategoriesByPostId(postId);
        // 根据帖子ID查询对应的标签信息
        BlogPostTags blogPostTags = blogPostTagsMapper.selectBlogPostTagsByPostId(postId);
        blogPostsVo.setCategorieId(blogPostCategories.getCategoryId());
        blogPostsVo.setTagId(blogPostTags.getTagId());
        return blogPostsVo;
    }

    /**
     * 查询文章列表
     * 
     * @param blogPostsVo 文章
     * @return 文章
     */
    @Override
    public List<BlogPostsVo> selectBlogPostsList(BlogPostsVo blogPostsVo)
    {
        // 创建BlogPosts实例，用于后续的数据映射和查询
        BlogPosts blogPosts = new BlogPosts();
        // 将blogPostsVo的属性值复制到blogPosts中，为查询准备条件
        BeanUtils.copyProperties(blogPostsVo,blogPosts);
        // 根据blogPosts的条件查询博客帖子列表
        List<BlogPosts> blogPostsList = blogPostsMapper.selectBlogPostsList(blogPosts);
        // 对查询结果进行流式处理，映射到BlogPostsVo对象，并添加分类和标签信息
        List<BlogPostsVo> collect = blogPostsList.stream().map(item -> {
            // 创建一个新的BlogPostsVo实例，用于存放处理后的数据
            BlogPostsVo blogPostsVo1 = new BlogPostsVo();
            // 将item的属性值复制到blogPostsVo1中
            BeanUtils.copyProperties(item, blogPostsVo1);
            // 根据帖子ID查询对应的分类信息
            BlogPostCategories blogPostCategories = blogPostCategoriesMapper.selectBlogPostCategoriesByPostId(item.getPostId());
            // 获取分类ID，并设置到blogPostsVo1中
            Long categoryId = blogPostCategories.getCategoryId();
            blogPostsVo1.setCategorieId(categoryId);
            // 根据帖子ID查询对应的标签信息
            BlogPostTags blogPostTags = blogPostTagsMapper.selectBlogPostTagsByPostId(item.getPostId());
            // 获取标签ID，并设置到blogPostsVo1中
            Long tagId = blogPostTags.getTagId();
            blogPostsVo1.setTagId(tagId);
            // 返回处理后的blogPostsVo1对象
            return blogPostsVo1;
        }).collect(Collectors.toList());
        // 返回处理后的博客帖子列表
        return collect;

    }

    /**
     * 新增文章
     * 
     * @param blogPostsVo 文章
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBlogPosts(BlogPostsVo blogPostsVo)
    {
        BlogPosts blogPosts = new BlogPosts();
        blogPostsMapper.insertBlogPosts(blogPosts);
        BeanUtils.copyProperties(blogPostsVo,blogPosts);
        BlogPostCategories blogPostCategories = new BlogPostCategories();
        blogPostCategories.setCategoryId(blogPostsVo.getCategorieId());
        blogPostCategories.setPostId(blogPostsVo.getPostId());
        blogPostCategoriesMapper.insertBlogPostCategories(blogPostCategories);
        BlogPostTags blogPostTags = new BlogPostTags();
        blogPostTags.setTagId(blogPostsVo.getTagId());
        blogPostTags.setPostId(blogPostsVo.getPostId());
        blogPostTagsMapper.insertBlogPostTags(blogPostTags);
        return 1;
    }

    /**
     * 修改文章
     * 
     * @param blogPostsVo 文章
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBlogPosts(BlogPostsVo blogPostsVo)
    {
        BlogPosts blogPosts = new BlogPosts();
        BeanUtils.copyProperties(blogPostsVo,blogPosts);
        blogPostsMapper.updateBlogPosts(blogPosts);
        BlogPostCategories blogPostCategories = new BlogPostCategories();
        blogPostCategories.setCategoryId(blogPostsVo.getCategorieId());
        blogPostCategoriesMapper.updateBlogPostCategoriesByPostId(blogPostCategories);
        BlogPostTags blogPostTags = new BlogPostTags();
        blogPostTags.setTagId(blogPostsVo.getTagId());
        blogPostTagsMapper.updateBlogPostTagsByPostId(blogPostTags);
        return 1;
    }

    /**
     * 批量删除文章
     * 
     * @param postIds 需要删除的文章主键
     * @return 结果
     */
    @Override
    public int deleteBlogPostsByPostIds(Long[] postIds)
    {
        return blogPostsMapper.deleteBlogPostsByPostIds(postIds);
    }

    /**
     * 删除文章信息
     * 
     * @param postId 文章主键
     * @return 结果
     */
    @Override
    public int deleteBlogPostsByPostId(Long postId)
    {
        return blogPostsMapper.deleteBlogPostsByPostId(postId);
    }
}
